﻿using AnesSystem.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace AnesSystem.ViewModels.Pacu.CQ.CQFY
{
  public  class PacuEventAndMedicineCtrlViewModel:ViewModels.PacuEventAndMedicineCtrlViewModel
    {

        #region Variables
        private List<EventAndDrug> _eventAndDrugCurPageList;

        #endregion


        #region Constructor
        public PacuEventAndMedicineCtrlViewModel(int page) : base(page)
        {
            EventAndDrugCurPageList = new List<EventAndDrug>();
            CurPage = page;
        }

        public List<EventAndDrug> EventAndDrugCurPageList
        {
            get
            {
                return _eventAndDrugCurPageList;
            }

            set
            {
                _eventAndDrugCurPageList = value;
            }
        }

        #endregion


        #region Properties






        #endregion


        #region 依赖属性


        #endregion


        #region Event


        #endregion


        #region PrivateMethods


        #endregion


        #region Public/ProtectedMethods
        public override void InitData()
        {
            DateTime pacuStartTime = DataManager.Instance.CurrentPatientLink.Status.PacuStartTime;
            DateTime startTime = pacuStartTime.AddHours((CurPage - 1) * 4);
            DateTime endTime = startTime.AddHours(4);
            if (!PatientLink.Status.OutPacuTime.Equals(new DateTime()) && endTime >=PatientLink.Status.OutPacuTime)
            {
                endTime = PatientLink.Status.OutPacuTime.AddSeconds(1);
            }
            AddSpecialDrug(EventAndDrugList);
            AddEvents(EventAndDrugList);

            List<EventAndDrug> CurPageList = new List<EventAndDrug>();

            foreach (var item in EventAndDrugList)
            {
                if (!item.IsDrug)
                {
                    item.IsOperStatus = IsOperStatus(item.PatientEvents.Code);
                    item.IsEdit = IsEdit(item.PatientEvents.Code);
                }

            }
            //排序
            EventAndDrugList.Sort((EventAndDrug o1, EventAndDrug o2) =>
            {
                if (o1.Time == o2.Time)
                {
                    if (o1.Name.Equals("入复苏室"))
                    {
                        return -1;
                    }
                    if (o2.Name.Equals("出复苏室"))
                    {
                        return -1;
                    }
                    if (!o1.IsOperStatus)
                    {
                        return -1;
                    }
                    if (o2.IsOperStatus)
                    {
                        return -1;
                    }
                    if (o1.InputTime < o2.InputTime)
                    {
                        return -1;
                    }
                    if (o1.InputTime > o2.InputTime)
                    {
                        return 1;
                    }
                    else
                    {
                        int ret = (o1.IsDrug ? o1.PatientDrugs.Id.ToString() : o1.PatientEvents.Id.ToString()).CompareTo(o2.IsDrug ? o2.PatientDrugs.Id.ToString() : o2.PatientEvents.Id.ToString());
                        if (ret > 0)
                        {
                            return 1;
                        }
                        else if (ret < 0)
                        {
                            return -1;
                        }
                        else
                        {
                            return 0;
                        }
                    }
                }
                else if (o1.Time < o2.Time)
                {
                    return -1;
                }
                else if (o1.Time > o2.Time)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            });
            CurPageList.Clear();
            for (int i = 0; i < EventAndDrugList.Count; i++)
            {
                EventAndDrugList[i].Index = i + 1;
                if (EventAndDrugList[i].Time >= startTime && EventAndDrugList[i].Time < endTime)
                {
                    CurPageList.Add(EventAndDrugList[i]);
                }
            }
            EventAndDrugCurPageList.AddRange(CurPageList);
            DrugList = new List<EventAndDrug>();
            EventList = new List<EventAndDrug>();
            foreach (var item in EventAndDrugCurPageList)
            {
                if (item.IsDrug)
                {
                    DrugList.Add(item);
                }
                else
                {
                    EventList.Add(item);
                }
            }

        }


        protected override void CreatShowCtrl()
        {

            for (int i = 0; i < EventAndDrugCurPageList.Count; i++)
            {
                AddEventAndDrugTextBlock(i);
            }
            CreatButton();
        }

        public virtual void AddEventAndDrugTextBlock(int index)
        {

            EventAndDrugItemCtrl ctrl = new EventAndDrugItemCtrl();
            ctrl.MouseLeftButtonDown += EventAndDrugCtrl_MouseDoubleClick;
            ctrl.CurFontSize = FontSize;
            string str = "";
            StringBuilder builder = new StringBuilder();
            if (EventAndDrugCurPageList[index].Index < 10)
            {
                str = "  " + EventAndDrugCurPageList[index].Index + "、" + EventAndDrugCurPageList[index].Time.ToString(DateFormat.DATE_FORMAT_STR_HM) + " ";
                for (int i = 0; i < (str.Length - 1) * 2; i++)
                {
                    builder.Append(" ");
                }
            }
            else
            {
                str = EventAndDrugCurPageList[index].Index + "、" + EventAndDrugCurPageList[index].Time.ToString(DateFormat.DATE_FORMAT_STR_HM) + " ";
                for (int i = 0; i < str.Length * 2; i++)
                {
                    builder.Append(" ");
                }
            }


            ctrl.Margin = new Thickness(0, 5, 10, 0);
            ctrl.DisplayTime = str;
            ctrl.DisplayName = builder.ToString() + EventAndDrugCurPageList[index].Name;
            ctrl.DataContext = EventAndDrugCurPageList[index];
            ctrl.MouseEnter += Ctrl_MouseEnter;
            EventAndDrugShowList.Add(ctrl);

        }

        protected override void Clear()
        {
            EventAndDrugList.Clear();
            EventAndDrugCurPageList.Clear();
            EventAndDrugShowList.Clear();
        }
        public override void Refresh()
        {
            Clear();
            InitData();
            CreatShowCtrl();
        }

        #endregion

    }
}

